package   org.eliza.base.color

import java.awt.Color

const val TplFgRGB = "38;2;%d;%d;%d"
const val TplBgRGB = "48;2;%d;%d;%d"
const val FgRGBPfx = "38;2;"
const val BgRGBPfx = "48;2;"


data class RGBA(val r: Int, val g: Int, val b: Int, val a: Int = 255) {}
data class HSL(val h: Int = 360, val s: Int = 100, val l: Int = 100, val a: Int = 255) {}
data class HWB(val hue: Int, val white: Int, val black: Int, val a: Int = 255) {}
data class CMYK(val c: Int, val m: Int, val y: Int, val k: Int) {}
typealias Hex = String


fun RGBA.HSB() {
    Color.RGBtoHSB(r, g, b, null)
}

fun RGBA.HEX(): String {
    return String.format("#%02x%02x%02x", r, g, b, a)
}

fun hueToRgb(t1: Int, t2: Int, hue: Int): Int {
    var hue = hue
    if (hue < 0) {
        hue += 6
    };
    if (hue >= 6) hue -= 6;
    if (hue < 1) return (t2 - t1) * hue + t1;
    else if (hue < 3) return t2;
    else if (hue < 4) return (t2 - t1) * (4 - hue) + t1;
    else return t1;
}

fun HSL.RGB(): RGBA {
    var t1: Int
    var t2: Int
    var h = h
    h = h / 60;
    if (l <= 0.5) {
        t2 = l * (s + 1);
    } else {
        t2 = l + s - (l * s);
    }
    t1 = l * 2 - t2;
    var r = hueToRgb(t1, t2, h + 2) * 255;
    var g = hueToRgb(t1, t2, h) * 255;
    var b = hueToRgb(t1, t2, h - 2) * 255;
    return RGBA(r, g, b)
}

fun HWB.RGB(): RGBA {
    var white = white
    var black = black
    var rgbArr: Array<Int> = Array(3) { 0 }
    val hsl = HSL(hue, 1, 50)
    var rgb = hsl.RGB();
    rgbArr[0] = rgb.r / 255;
    rgbArr[1] = rgb.g / 255;
    rgbArr[2] = rgb.b / 255;
    var tot = white + black;
    if (tot > 1) {
        white = white / tot
        black = black / tot
    }
    for (i in 0 until 3) {
        rgbArr[i] *= (1 - (white) - (black));
        rgbArr[i] += (white);
        rgbArr[i] = rgbArr[i] * 255;
    }
    return RGBA(rgbArr[0], rgbArr[1], rgbArr[2])
}

fun CMYK.RGB(): RGBA {
    var r = 255 - ((1.coerceAtMost(c * (1 - k) + k)) * 255);
    var g = 255 - ((1.coerceAtMost(m * (1 - k) + k)) * 255);
    var b = 255 - ((1.coerceAtMost(y * (1 - k) + k)) * 255);
    return RGBA(r, g, b)
}

///==============
fun RGBA.HSL(): HSL {
//    var min, max, i, l, s, maxcolor, h, rgb = [];

    var rgb = arrayOf(r / 255, g / 255, b / 255)
    var min = rgb[0];
    var max = rgb[0];
    var maxcolor = 0;
    for (i in 0..rgb.size) {

        if (rgb[i + 1] <= min) {
            min = rgb[i + 1];
        }
        if (rgb[i + 1] >= max) {
            max = rgb[i + 1];maxcolor = i + 1;
        }
    }
    var h: Int = 0
    if (maxcolor == 0) {
        h = (rgb[1] - rgb[2]) / (max - min);
    }
    if (maxcolor == 1) {
        h = 2 + (rgb[2] - rgb[0]) / (max - min);
    }
    if (maxcolor == 2) {
        h = 4 + (rgb[0] - rgb[1]) / (max - min);
    }
    var l: Int = 0
    var s: Int = 0

    h = h * 60;
    if (h < 0) {
        h = h + 360; }
    l = (min + max) / 2;
    if (min == max) {
        s = 0;
    } else {
        if (l < 0.5) {
            s = (max - min) / (max + min);
        } else {
            s = (max - min) / (2 - max - min);
        }
    }
    s = s;
    return HSL(h, s, l);
}


private  fun main() {
    val toHex = RGBA(255, 255, 255, 255).HEX()
    println(toHex)
}

